package com.ruoyi.lession.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.lession.common.ResultInfo;
import com.ruoyi.lession.entily.*;
import com.ruoyi.lession.mapper.QuestMapper;
import com.ruoyi.lession.service.QuestService;
import com.ruoyi.lession.utils.ExcelUtils;
import com.ruoyi.lession.utils.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 朱鑫杰
 * 问卷管理
 */
@RestController
@RequestMapping("quest")
public class QuestController {

    @Resource
    QuestService questService;

    @Resource
    QuestMapper questMapper;

    /**
     * 问卷管理列表页
     * @param pageNum
     * @param pageSize
     * @return
     * @author 朱鑫杰
     */
    @RequestMapping("findByList/{pageNum}/{pageSize}")
    public ResultInfo lessonlist(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize){
        ResultInfo resultInfo =questService.findByQuestList(pageNum,pageSize);
        return resultInfo;
    }

    /**
     *
     * @param quest
     * @return
     * @author 朱鑫杰
     */
    @RequestMapping("QuestInsert")
    public ResultInfo QuertInsert(@RequestBody Quest quest){
        ResultInfo resultInfo =questService.insert(quest);
        return resultInfo;
    }

    /**
     * 文件状态修改为以结束状态
     * @param quest
     * @return
     * @author 朱鑫杰
     */
    @RequestMapping("QuestUpdate")
    public ResultInfo QuestUpdateStutas(@RequestBody Quest quest){
        ResultInfo resultInfo =questService.QuestUpdateStutas(quest);
        return resultInfo;
    }

    /**
     *查询题目
     * @return
     * @author 朱鑫杰
     */
    @RequestMapping("titleList")
    public List<Title> titleList(){
        return questService.findByTitleList();
    }

    /**
     *添加问卷调查
     * @param questionnaireAddUtil
     * @author 朱鑫杰
     */
    @RequestMapping("/titleAdd")
    public void questionnaireAdd(@RequestBody QuestionnaireAddUtil questionnaireAddUtil){
        Integer[] tid = questionnaireAddUtil.getTid();

        questService.save(questionnaireAddUtil);
        List<QuestTitle> questionnaireTopics = new ArrayList<>();
        for (int i = 0; i < tid.length; i++) {
            QuestTitle questionnaireTopic = new QuestTitle();
            questionnaireTopic.setId(questionnaireAddUtil.getId());
            questionnaireTopic.setTid(tid[i].longValue());
            questionnaireTopics.add(questionnaireTopic);
        }
        questService.saveBatch(questionnaireTopics);
    }

    /**
     * 删除问卷
     * @param id
     * @return
     * @author 朱鑫杰
     */
    @RequestMapping("/questdelete")
    public ResultInfo questdelete(@RequestParam("id") Long id){
        ResultInfo resultInfo = questService.deleteId(id);
        return resultInfo;
    }

    /**
     *
     * @param id
     * @return问卷测评
     * @author 朱鑫杰
     */
    @RequestMapping("titleContentList")
    public List<TitleContentUtil> topicUtilList(@RequestParam("id") Long id){
        List<TitleContentUtil> topicChoice = questService.topicUtilList(id);
        return topicChoice;
    }

    /**
     *
     * @param contentSumList
     * @return计算测评分数
     *  @author 朱鑫杰
     */
    @RequestMapping("/sumcnum")
    public ResultInfo sumcnum(@RequestBody List<ContentSum> contentSumList){
       ResultInfo resultInfo  = questService.sumcum(contentSumList);
       return  resultInfo;
    }


    /**
     * 生成健康测评报告和健康方案
     * @param cnum
     * @return
     * @author 朱鑫杰
     */
    @RequestMapping("report")
    public ResultInfo reports(@RequestParam("cnum") Integer cnum){
        ResultInfo resultInfo = questService.repoetList(cnum);
        return resultInfo;
    }

    /**
     *根据健康报告查询课程及视频
     * @param rid
     * @return
     * @author 朱鑫杰
     */
    @RequestMapping("/lessonReportList")
    public ResultInfo lessonReportList(@RequestParam("rid") Long rid){
        ResultInfo resultInfo =questService.findByLessonReportList(rid);
        return resultInfo;
    }


    /**
     *创建课程订单
     * @param order
     * @param request
     * @return
     * @author 朱鑫杰
     */
    @RequestMapping("/orderAdd")
    public Integer orderFormAdd(@RequestBody Order order, HttpServletRequest request){
        Integer resultInfo =questService.orderFromInsert(order,request);
        return resultInfo;
    }

    /**
     * Execl表格导出扯平数据表
     * @param ids
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("exportPoi")
    public ResponseEntity<byte[]> exportExcel(Long ids) throws IOException {
        List<Quest> treeList1 =questService.findQuestListByIds(ids);
        String[] title={"id","题目名称","题目简介","创建时间","结束时间","状态 "};
        List<TitleContent> treeList2 =questService.findTitleCotentListByIds(ids);
        String[] titles={"id","选项名称","所属题目","分数"};
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFSheet sheet1 = workbook.createSheet();
        HSSFRow row = sheet.createRow(0);
        HSSFRow rows = sheet1.createRow(0);
        HSSFCell cell = null;
        for (int i=0;i<title.length;i++){
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }
        HSSFCell cells = null;
        for (int j=0;j<titles.length;j++){
            cell = rows.createCell(j);
            cell.setCellValue(titles[j]);
        }
        for (int i=0; i<treeList1.size();i++){
            HSSFRow row1 = sheet.createRow(i+1);
            HSSFCell cell1 = row1.createCell(0);
            cell1.setCellValue(treeList1.get(i).getId());
            HSSFCell cell3 = row1.createCell(1);
            cell3.setCellValue(treeList1.get(i).getName());
            HSSFCell cell4 = row1.createCell(2);
            cell4.setCellValue(treeList1.get(i).getNote());
            HSSFCell cell5 = row1.createCell(3);
            cell5.setCellValue(treeList1.get(i).getEndtime());
            HSSFCell cell6 = row1.createCell(4);
            cell6.setCellValue(treeList1.get(i).getCreattime());
            HSSFCell cell7 = row1.createCell(5);
            cell7.setCellValue(treeList1.get(i).getStatus());

        }
        for (int j=0; j<treeList2.size();j++){
            HSSFRow row1 = sheet1.createRow(j+1);
            HSSFCell cell1 = row1.createCell(0);
            cell1.setCellValue(treeList2.get(j).getCid());
            HSSFCell cell3 = row1.createCell(1);
            cell3.setCellValue(treeList2.get(j).getCname());
            HSSFCell cell4 = row1.createCell(2);
            cell4.setCellValue(treeList2.get(j).getTid());
            HSSFCell cell5 = row1.createCell(3);
            cell5.setCellValue(treeList2.get(j).getNum());

        }
        //我这里是先固定了一个要导出的地址：实际下载地址自己在下载时选择
        String pathname = "C:\\Users\\l\\Desktop\\2.xlsx";
        File file = new File(pathname);
        file.createNewFile();
        FileOutputStream fileOutputStream = FileUtils.openOutputStream(file);
        workbook.write(fileOutputStream);
        return FileUtil.FileDownload(pathname);

    }


    @RequestMapping("enterPoi")
    @ResponseBody
    public Boolean enterPoi(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipart= (MultipartHttpServletRequest) request;
        MultipartFile file = multipart.getFile("upfile");
        if(file.isEmpty()){
            throw new Exception("文件不存在");
        }
        InputStream in = file.getInputStream();
        List<List<Object>> bankListByExcel2 = new ExcelUtils().getBankListByExcel2(in, file.getOriginalFilename());
        System.out.println(bankListByExcel2);
        List<Quest> quests = new ArrayList<>();
        for (int i=0;i<bankListByExcel2.size();i++){
            List<Object> objects = bankListByExcel2.get(i);
            Quest quest = new Quest();
            quest.setId((Long) objects.get(1));
            quest.setName((String) objects.get(2));
            quest.setNote((String) objects.get(3));
            quest.setCreattime((String) objects.get(4));
            quest.setEndtime((String) objects.get(5));
            quest.setStatus((String) objects.get(3));
            quests.add(quest);
        }

        try {
            questService.savePoi(quests);
            in.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    
}
